package com.etb.wisdom.eframe.rights.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.etb.core.service.ETBService;
import com.etb.core.utils.DataUtil;
import com.etb.web.eui.tree.TreeNode;
import com.etb.web.eui.tree.TreeNode.State;
import com.etb.web.eui.utils.TreeUtil;
import com.etb.wisdom.eframe.auth.AuthConstant.AuthType;
import com.etb.wisdom.eframe.auth.AuthConstant.PartyType;
import com.etb.wisdom.eframe.auth.AuthConstant.ResType;
import com.etb.wisdom.eframe.dict.DictConstant;
import com.etb.wisdom.eframe.model.EtbPartyAuth;
import com.etb.wisdom.eframe.model.EtbPartyRes;
import com.etb.wisdom.eframe.model.OrgEmployee;
import com.etb.wisdom.eframe.model.OrgOrganization;
import com.etb.wisdom.eframe.model.OrgPosition;
import com.etb.wisdom.eframe.org.service.OrgEmployeeService;
import com.etb.wisdom.eframe.org.service.OrgOrganizationService;
import com.etb.wisdom.eframe.org.service.OrgPositionService;
import com.etb.wisdom.eframe.rights.service.EtbRoleService;

@Service
public class EtbRoleServiceImpl extends ETBService implements EtbRoleService {
	
	@Autowired
	public OrgOrganizationService orgService;
	@Autowired
	public OrgPositionService posiService;
	@Autowired
	public OrgEmployeeService empService;
	
	@Override
	public List<TreeNode> getAllOrg(String roleId) {
		//获取和参数roleId有关系的机构
		EtbPartyAuth template = new EtbPartyAuth();
		template.setPartytype(PartyType.organization.toString());
		template.setAuthid(roleId);
		template.setAuthtype(AuthType.role.toString());
		List partyIds = service.querySpecifiedProperty(template, "partyid");
		//获取所有机构
		List<OrgOrganization> orgs = orgService.queryAll();
		Map<String,String> variable = new HashMap<String,String>();
		variable.put("id", "id");
		variable.put("pid", "uplevelid");
		variable.put("text", "orgname");
		Map<String,Object> constant = new HashMap<String,Object>();
		constant.put("state", State.open.toString());
		constant.put("iconCls", "icon-org");
		constant.put("checkedProperty", "id");
		constant.put("checkedValue", partyIds);
		
		return TreeUtil.asTreeNode(orgs, variable, constant);
	}
	
	@Override
	public List<TreeNode> getAllPosi(String roleId) {
		//获取和参数roleId有关系的岗位
		EtbPartyAuth template = new EtbPartyAuth();
		template.setPartytype(PartyType.position.toString());
		template.setAuthid(roleId);
		template.setAuthtype(AuthType.role.toString());
		List partyIds = service.querySpecifiedProperty(template, "partyid");
		//先获取机构
		List<OrgOrganization> orgs = orgService.queryAll();
		Map variable = new HashMap<String,String>();
		variable.put("id", "id");
		variable.put("pid", "uplevelid");
		variable.put("text", "orgname");
		Map constant = new HashMap<String,String>();
		constant.put("nodeType", OrgOrganizationService.TREENODETYPE_ORGANIZATION);
		constant.put("state", State.open.toString());
		constant.put("iconCls", "icon-org");
		List<TreeNode> treeNodes = TreeUtil.asTreeNode(orgs, variable, constant);
		//再获取机构下的一级岗位
		List<OrgPosition> posis = posiService.queryAllOneLevel();
		variable.put("pid", "orgid");
		variable.put("text", "posiname");
		constant.put("nodeType", OrgOrganizationService.TREENODETYPE_POSITION);
		constant.put("iconCls", "icon-position");
		constant.put("checkedProperty", "id");
		constant.put("checkedValue", partyIds);
		treeNodes.addAll(TreeUtil.asTreeNode(posis, variable, constant));
		//获取除一级岗位以外所有岗位
		posis = posiService.queryAllNotOneLevel();
		variable.put("pid", "uplevelid");
		treeNodes.addAll(TreeUtil.asTreeNode(posis, variable, constant));
		return treeNodes;
	}
	
	@Override
	public List<TreeNode> getAllEmp(String roleId) {
		//获取和参数roleId有关系的机构
		EtbPartyAuth template = new EtbPartyAuth();
		template.setPartytype(PartyType.employee.toString());
		template.setAuthid(roleId);
		template.setAuthtype(AuthType.role.toString());
		List partyIds = service.querySpecifiedProperty(template, "partyid");
		//先获取机构
		List<OrgOrganization> orgs = orgService.queryAll();
		Map variable = new HashMap<String,String>();
		variable.put("id", "id");
		variable.put("pid", "uplevelid");
		variable.put("text", "orgname");
		Map constant = new HashMap<String,String>();
		constant.put("nodeType", OrgOrganizationService.TREENODETYPE_ORGANIZATION);
		constant.put("state", State.open.toString());
		constant.put("iconCls", "icon-org");
		List<TreeNode> treeNodes = TreeUtil.asTreeNode(orgs, variable, constant);
		//再获取机构下的一级岗位
		List<OrgPosition> posis = posiService.queryAllOneLevel();
		variable.put("pid", "orgid");
		variable.put("text", "posiname");
		constant.put("nodeType", OrgOrganizationService.TREENODETYPE_POSITION);
		constant.put("iconCls", "icon-position");
		treeNodes.addAll(TreeUtil.asTreeNode(posis, variable, constant));
		//获取除一级岗位以外所有岗位
		posis = posiService.queryAllNotOneLevel();
		variable.put("pid", "uplevelid");
		treeNodes.addAll(TreeUtil.asTreeNode(posis, variable, constant));
		//随后获取机构下的员工
		List<OrgEmployee> emps = empService.queryAll();
		for(OrgEmployee emp: emps) {
			TreeNode node = new TreeNode(emp.getId(), emp.getEmpname());
			if(StringUtils.isBlank(emp.getPositionid())) {
				node.setPid(emp.getOrgid());
			} else {
				node.setPid(emp.getPositionid());
			}
			if(partyIds.contains(emp.getId())) {
				node.setChecked("true");
			}
			node.put("nodeType", OrgOrganizationService.TREENODETYPE_EMPLOYEE);
			node.setIconCls(DictConstant.GENDER_GIRL.equals(emp.getGender()) ? "icon-girl" : "icon-boy");
			treeNodes.add(node);
		}
		return treeNodes;
	}

	@Override
	public void saveFunc(String roleId, String addFuncIds, String delFuncIds) {
		List<String> addIdList = DataUtil.split(addFuncIds);
		List<String> delIdList = DataUtil.split(delFuncIds);
		for(String id: addIdList) {
			EtbPartyRes res = new EtbPartyRes();
			res.setPartyid(roleId);
			res.setPartytype(AuthType.role.toString());
			res.setResid(id);
			res.setRestype(ResType.function.toString());
			service.save(res);
		}
		for(String id: delIdList) {
			EtbPartyRes res = new EtbPartyRes();
			res.setPartyid(roleId);
			res.setPartytype(AuthType.role.toString());
			res.setResid(id);
			res.setRestype(ResType.function.toString());
			service.delete(res);
		}
		
	}
	
	@Override
	public void saveEmp(String roleId, String addEmpIds, String delEmpIds) {
		List<String> addIdList = DataUtil.split(addEmpIds);
		List<String> delIdList = DataUtil.split(delEmpIds);
		for(String id: addIdList) {
			EtbPartyAuth auth = new EtbPartyAuth();
			auth.setPartyid(id);
			auth.setPartytype(PartyType.employee.toString());
			auth.setAuthid(roleId);
			auth.setAuthtype(AuthType.role.toString());
			service.save(auth);
		}
		for(String id: delIdList) {
			EtbPartyAuth auth = new EtbPartyAuth();
			auth.setPartyid(id);
			auth.setPartytype(PartyType.employee.toString());
			auth.setAuthid(roleId);
			auth.setAuthtype(AuthType.role.toString());
			service.delete(auth);
		}
	}
	
	@Override
	public void savePosi(String roleId, String addPosiIds, String delPosiIds) {
		List<String> addIdList = DataUtil.split(addPosiIds);
		List<String> delIdList = DataUtil.split(delPosiIds);
		for(String id: addIdList) {
			EtbPartyAuth auth = new EtbPartyAuth();
			auth.setPartyid(id);
			auth.setPartytype(PartyType.position.toString());
			auth.setAuthid(roleId);
			auth.setAuthtype(AuthType.role.toString());
			service.save(auth);
		}
		for(String id: delIdList) {
			EtbPartyAuth auth = new EtbPartyAuth();
			auth.setPartyid(id);
			auth.setPartytype(PartyType.position.toString());
			auth.setAuthid(roleId);
			auth.setAuthtype(AuthType.role.toString());
			service.delete(auth);
		}
	}
	
	@Override
	public void saveOrg(String roleId, String addOrgIds, String delOrgIds) {
		List<String> addIdList = DataUtil.split(addOrgIds);
		List<String> delIdList = DataUtil.split(delOrgIds);
		for(String id: addIdList) {
			EtbPartyAuth auth = new EtbPartyAuth();
			auth.setPartyid(id);
			auth.setPartytype(PartyType.organization.toString());
			auth.setAuthid(roleId);
			auth.setAuthtype(AuthType.role.toString());
			service.save(auth);
		}
		for(String id: delIdList) {
			EtbPartyAuth auth = new EtbPartyAuth();
			auth.setPartyid(id);
			auth.setPartytype(PartyType.organization.toString());
			auth.setAuthid(roleId);
			auth.setAuthtype(AuthType.role.toString());
			service.delete(auth);
		}
	}
	
}
